<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\PictureRequest;
use App\Models\Picture;
use App\Transformers\PictureTransformer;
use App\Transformers\WalletTransformer;
use Illuminate\Http\Request;

class PictureController extends Controller
{
    public function index()
    {
        $data = Picture::where('user_id', $this->user->id)->get();

        return $this->response->collection($data, new PictureTransformer());
    }

    public function create(PictureRequest $request)
    {
        $data = $request->only(['picture']);

        $result = [];

        if (is_array($data['picture'])) {
            foreach ($data['picture'] as $picture) {
                $res = $this->user->pictures()->create(['picture' => $picture]);

                $res['picture'] = image_url($res['picture']);

                array_push($result, $res);
            }
        } else {
            $result = $this->user->pictures()->create($data);

            $result['picture'] = image_url($result['picture']);
        }

        if ($result) {
            return response()->json(['message' => trans('api.save_succeed'), 'code' => 1, 'status' => true, 'data' => $result], 200);
        } else {
            return response()->json(['message' => trans('api.save_failed'), 'code' => 0, 'status' => false], 200);
        }
    }

    public function delete(Picture $picture, $id)
    {
        $res = $picture->destroy($id);

        if ($res) {
            return response()->json(['message' => trans('api.delete_succeed'), 'code' => 1, 'status' => true, 'data' => ''], 200);
        } else {
            return response()->json(['message' => trans('api.delete_failed'), 'code' => 0, 'status' => false], 200);
        }
    }
}
